﻿class DataInsert(object):
    def __init__(self, conn):
        self.conn = conn

    # 奇安信网神防火墙数据插入
    def insert_data(self, record_datetime, record_permit_count, record_deny_count, pre_date):
        cursor = self.conn.cursor()

        query_sql = "select * from web_qianxingfw " \
                    "where DATE_FORMAT(create_date,'%Y-%m-%d') = DATE_FORMAT('{}','%Y-%m-%d') " \
                    "order by id desc limit 1".format(pre_date)
        cursor.execute(query_sql)
        result = cursor.fetchall()
        if len(result) < 1:
            params = (record_permit_count, record_deny_count, record_datetime)
            insert_sql = 'insert into web_qianxingfw(permit_count,deny_count,create_date) ' \
                         'values("%s","%s","%s")' % params
        else:

            pre_permit_count = result[0][1]
            pre_deny_count = result[0][2]

            current_permit_count = record_permit_count - pre_permit_count
            current_deny_count = record_deny_count - pre_deny_count
            params = (record_permit_count, record_deny_count, record_datetime, current_permit_count, current_deny_count)
            insert_sql = 'insert into web_qianxingfw(permit_count,deny_count,create_date,current_permit_count,current_deny_count) ' \
                         'values("%s","%s","%s","%s","%s")' % params
        try:
            cursor.execute(insert_sql)
            self.conn.commit()
        except Exception as ex:
            self.conn.rollback()
            print('insert_data_error:', ex)
        finally:
            cursor.close()

    def insert_k01_attack_count(self, data):
        import datetime
        now = datetime.datetime.now()
        current_time = now.time()
        specific_time = datetime.time(hour=15, minute=00, second=0, microsecond=0)
        if current_time > specific_time:
            create_date = now + datetime.timedelta(days=1)
        else:
            create_date = now

        cursor = self.conn.cursor()
        query_sql = f'select * from web_k01 where DATE_FORMAT(update_datetime,"%Y-%m-%d") = "{create_date.strftime("%Y-%m-%d")}"'
        cursor.execute(query_sql)
        result = cursor.fetchall()
        atk_total = data["atk_total"][-1]
        atkip_total = data['atkip_total'][-1]
        outgoing_total = data['outgoing_total'][-1]
        if len(result) < 1:
            sql = 'insert into web_k01(atk_count, atkip_count, outgoing_count,create_datetime,update_datetime)' \
                  'values("%s","%s","%s","%s","%s")' % \
                  (atk_total, atkip_total, outgoing_total, create_date,now)
        else:
            sql = f'update web_k01 set atk_count={atk_total},atkip_count={atkip_total},outgoing_count={outgoing_total},' \
                  f'update_datetime="{now}"' \
                  f' where DATE_FORMAT(create_datetime,"%Y-%m-%d") = "{create_date.strftime("%Y-%m-%d")}"'
        print(sql)
        import json
        with open('sql.txt', 'a') as f:
            f.write(json.dumps(sql, ensure_ascii=False) + "\n")
        try:
            cursor.execute(sql)
            self.conn.commit()
        except Exception as ex:
            self.conn.rollback()
            print('insert_data_error:', ex)
        finally:
            cursor.close()
